<?php
namespace app\common\service;;

use think\facade\Db;

class Supply extends Base {
    static $AUTH_CHECK = false;
    public function lists(){
        $pagenum = input('pagenum')?:1;
        $listsrow = 10;
        $lists = Db::name('suppliers')->where('1=1')->page($pagenum,$listsrow)->select();
        $clist = Db::name('supply_category')->where('status',1)->select();
        $result = [
            'status' => true,
            'msg' => '查询成功！',
            'data' => $lists,
            'clist' => $clist
        ];
        return $result;
    }
    public function get_lists(){
        $pagenum = input('pagenum')?:1;
        $listsrow = 10;
        $where = [];
        if(input('cid')){
            $where['cid'] = input('cid');
        }
        if(input('keyword')){
            $where['name'] = ['like','%'.input('keyword').'%'];
        }
        $lists = Db::name('suppliers')->where($where)->page($pagenum,$listsrow)->select();
        if($lists){
            foreach($lists as $k=>$v){
                $prolist = Db::name('goods')->where(['sid'=>$v['id']])->limit(3)->select();
                $lists[$k]['prolist'] = $prolist;
            }
        }
        $result = [
            'status' => true,
            'msg' => '查询成功！',
            'datalist' => $lists,
        ];
        return $result;
    }
    public function detail(){
        $id = input('id');
        $supply_info = Db::name('suppliers')->where('id',$id)->find();
        if($id && !$supply_info){
            return [
                'status' => 0,
                'msg' => '商家不存在'
            ];
        }
        $bset = [
            'show_product' => 1,
            'show_comment' => 1,
            'show_detail' => 1
        ];
        $pics = explode(',',$supply_info['pics']);
        $supply_info['content'] = htmlspecialchars_decode($supply_info['content']);
        $sales_total = Db::name('goods')->where('sid',$id)->sum('sales_sum');
        $supply_info['sales'] = $sales_total;

        $goods_num = Db::name('goods')->where('sid',$id)->where('is_on_sale',1)->count();
        $supply_info['goods_num'] = $goods_num;
        $map = [];
        $map['c.sid'] = $id;
        $field = 'c.*,u.nickname,u.head_pic';
        $countcomment = Db::name('comment')
            ->alias('c')
            ->join('users u','u.user_id=c.user_id')
            ->where($map)
            ->order('c.comment_id desc')
            ->field($field)->count();
//            ->paginate(15,false,['page'=>$page,'list_rows'=>15]);
//        if($lists->total()){
//            $domain_url = getCurUrl();
//            foreach($lists as $k=>$v){
//                $v['img'] = explode(',',$v['img']);
//                $v['head_pic'] = $v['head_pic']?:$domain_url.'/static/img/touxiang.png';
//                $v['nickname'] = $v['nickname']?:$v['username'];
//                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
//                $lists->offsetSet($k,$v);
//            }
//        }
        $result = [
            'status' => true,
            'msg' => '查询成功！',
            'supply_info' => $supply_info,
            'bset' => $bset,
            'countcomment' => $countcomment,
            'isdiy' => 0,
            'pics' => $pics
        ];
        return $result;
    }
    public function apply(){
        $user_id = $this->user_id;
        if(IS_POST){
            $params = input('info/a');
            $data = [];
            $data['user_id'] = $user_id;
            $data['username'] = $params['un'];
            if(!empty($params['pwd'])){
                if($params['pwd']!=$params['repwd']){
                    return ['status'=>0,'msg'=>'两次密码输入不一致！'];
                }
                $data['pwd'] = encrypt($params['pwd']);
            }
            $data['name'] = $params['name']??'';
            $data['logo'] = $params['pic']??'';
            $data['desc'] = $params['desc']??'';
            $data['nickname'] = $params['nickname']??'';
            $data['mobile'] = $params['mobile']??'';
            $data['pics'] = $params['pics']??'';
            $data['zhengming'] = $params['zhengming']??'';
            $data['status'] = $params['status']??'';
            $data['w_time'] = time();
            if(!empty($params['id'])){
                Db::name('suppliers')->where('id',$params['id'])->update($data);
                $admin_id = Db::name('admin')->where('suppliers_id',$params['id'])->value('admin_id');
                if($admin_id){
                    $data_a = [];
                    $data_a['user_name'] = $params['un'];
                    if(!empty($data['pwd'])){
                        $data_a['password'] = $data['pwd'];
                    }
                    Db::name('admin')->where('admin_id',$admin_id)->update($data_a);
                }else{
                    $supply_id = Db::name('suppliers')->insertGetId($data);
                    $role_id = Db::name('admin_role')->where('is_supply',1)->value('role_id');
                    //插入一个admin数据
                    Db::name('admin')->insert([
                        'user_name' => $params['un'],
                        'password' => encrypt($params['pwd']),
                        'add_time' => time(),
                        'role_id' => $role_id,
                        'suppliers_id' => $supply_id
                    ]);
                }

            }else{
                $supply_id = Db::name('suppliers')->insertGetId($data);
                $role_id = Db::name('admin_role')->where('is_supply',1)->value('role_id');
                //插入一个admin数据
                Db::name('admin')->insert([
                    'user_name' => $params['un'],
                    'password' => encrypt($params['pwd']),
                    'add_time' => time(),
                    'role_id' => $role_id,
                    'suppliers_id' => $supply_id
                ]);
            }
            return ['status'=>1,'msg'=>'申请成功，请等待平台审核！'];
        }else{

            $info = Db::name('suppliers')->where('user_id',$user_id)->find();
            $clist = Db::name('supply_category')->where('status',1)->select();
            $bset = [
                'deposit' => 0,
                'xieyi_name' =>  tpCache('xieyi.supply_xieyi_name'),
                'xieyi' =>  tpCache('xieyi.supply_xieyi_content'),
                'xieyi_show' => 1
            ];
            $result = [
                'status' => true,
                'msg' => '查询成功！',
                'clist' => $clist,
                'info' => $info?:[],
                'bset' => $bset
            ];
            return $result;
        }
    }

    //获取商品列表 评价列表
    public function getdatalist(){
        $id = input('param.sid/d');
        $st = input('param.st/d');
        $pagenum = input('param.pagenum');
        if(!$pagenum) $pagenum = 1;
        if($st == 0){//商品
            $pernum = 20;
            $where = [];
            $price_tag = $cost_tag = '￥';
            $price_color = $cost_color = '';
            $show_sellprice = true;
            $show_cost = false;
            $hidecart = false;
            $where['is_on_sale'] = 1;
            if($id){
                $where['sid'] = $id;
            }
            $prolist = Db::name('goods')->where($where)->page($pagenum,$pernum)->order('goods_id desc')->select();
            foreach ($prolist as $v=>&$value){
                $value['price_tag'] = $price_tag;
                $value['price_color'] = $price_color;
                $value['cost_tag'] = $cost_tag;
                $value['cost_color'] = $cost_color;
                $value['show_sellprice'] = $show_sellprice;
                $value['show_cost'] = $show_cost;
                $value['hide_cart'] = $hidecart;
                $value['pic'] = $value['original_img'];
            }
            if(!$prolist) $prolist = [];
            if(request()->isPost()){
                return ['status'=>1,'data'=>$prolist];
            }
        }else{//评价
            $pernum = 10;
            $map = [];
            $map['c.sid'] = $id;
            $field = 'c.*,u.nickname,u.head_pic';
            $lists = Db::name('comment')
                ->alias('c')
                ->join('users u','u.user_id=c.user_id')
                ->where($map)
                ->order('c.comment_id desc')
                ->field($field)
            ->paginate(15,false,['page'=>$pagenum,'list_rows'=>15]);
            if($lists->total()){
                $domain_url = getCurUrl();
                foreach($lists as $k=>$v){
                    $v['img'] = explode(',',$v['img']);
                    $v['head_pic'] = $v['head_pic']?:$domain_url.'/static/img/touxiang.png';
                    $v['nickname'] = $v['nickname']?:$v['username'];
                    $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                    $lists->offsetSet($k,$v);
                }
            }
            return ['status'=>1,'data'=>$lists->items()];
        }
    }
}
